package com.zztpc.mzb.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import com.google.gson.Gson;
import com.zztpc.mzb.myapplication.entity.Carlist;
import com.zztpc.mzb.myapplication.entity.Che;
import com.zztpc.mzb.myapplication.servirce.Cheming;
import com.zztpc.mzb.myapplication.servirce.Chepai;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
      //  getCars();
        getcheliangPinpai();
    }

    private void getcheliangPinpai() {
        //创建retrofit对象
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://apis.juhe.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Cheming cheming = retrofit.create(Cheming.class);
        Call<Che> allche = cheming.getAllche("e2a68c9e52b375c386286cf9747bf6d9", "A");
        allche.enqueue(new Callback<Che>() {
            @Override
            public void onResponse(Call<Che> call, Response<Che> response) {
                Che ch=response.body();
                Log.d(TAG, "onResponse: "+ch.getResult().get(0).getBrand_name());

            }

            @Override
            public void onFailure(Call<Che> call, Throwable t) {
                Log.d(TAG, "onFailure: "+t.getMessage());
            }
        });

    }

    private void getCars() {
        //创建retrofit对象
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://www.hnzzsgz.cn:5001/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        //创建接口对象
        final Chepai chepai=retrofit.create(Chepai.class);
        //调用接口中的抽象方法
        Call<Carlist>all=chepai.getAll();
        //调用Call类型中的异步方法
        all.enqueue(new Callback<Carlist>() {
            @Override
            public void onResponse(Call<Carlist> call, Response<Carlist> response) {
                Carlist body=response.body();
                Log.d(TAG, "onResponse: "+new Gson().toJson(body));
               // Log.d(TAG, "onResponse: "+body.getData().get(0).getCarplate());
            }

            @Override
            public void onFailure(Call<Carlist> call, Throwable t) {
                Log.d(TAG, "onFailure: "+t.getMessage());
            }
        });
    }
}
